package com.yuneec.fly.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.yuneec.fly.ConstantValue;

/**
 * SharePreferences操作工具类
 */
public class SharePrefUtil {
    private static String tag = SharePrefUtil.class.getSimpleName();
    private final static String SP_NAME = ConstantValue.SHAREDPREFERENCES_NAME;
    private static SharedPreferences sp;

    public interface KEY {
        String KEY_CONNECTED_CAMERA_SSID = "connected_camera_ssid";
        String KEY_CONNECTED_CAMERA_PASSWORD = "connected_camera_password";
        String KEY_CONNECTED_CAMERA_TYPE = "connected_camera_type";
        String KEY_LAST_MAP_TYPE = "last_map_type";
        String KEY_LAST_CONTROLLER_LATITUDE = "last_controller_latitude";
        String KEY_LAST_CONTROLLER_LONGITUDE = "last_controller_longitude";
        String KEY_WAYPOINTS_GUIDE_SHOWED = "waypoints_guide_showed";
        String KEY_FENCE_GUIDE_SHOWED = "fence_guide_showed";
        String KEY_MAIN_GUIDE_SHOWED = "main_guide_showed";
        String KEY_SELECTED_MAP_TYPE = "selected_map_type";//0:auto 1:google map 2:amap
        String KEY_LAST_CONNECTED_DRONE_RX = "last_connected_drone_rx";
    }

    /**
     * 保存布尔值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putBoolean(key, value).commit();
    }

    /**
     * 保存字符串
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveString(Context context, String key, String value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putString(key, value).commit();

    }

    public static void clear(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().clear().commit();
    }

    /**
     * 保存long型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveLong(Context context, String key, long value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putLong(key, value).commit();
    }

    /**
     * 保存int型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveInt(Context context, String key, int value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putInt(key, value).commit();
    }

    /**
     * 保存float型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveFloat(Context context, String key, float value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putFloat(key, value).commit();
    }

    /**
     * 获取字符值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getString(key, defValue);
    }

    /**
     * 获取int值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getInt(key, defValue);
    }

    /**
     * 获取long值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static long getLong(Context context, String key, long defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getLong(key, defValue);
    }

    /**
     * 获取float值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static float getFloat(Context context, String key, float defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getFloat(key, defValue);
    }

    /**
     * 获取布尔值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getBoolean(Context context, String key,
                                     boolean defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getBoolean(key, defValue);
    }

}
